//
//  ImageDownloader.m
//  Douban
//
//  Created by lanou3g on 15-3-12.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ImageDownloader.h"

@interface ImageDownloader ()
@property(nonatomic,retain)NSMutableData *receiveData;
@property(nonatomic,retain)NSString *urlString;
@property(nonatomic,assign)NSInteger tag;
@property(nonatomic,assign)id<ImageDownloaderDelegate> delegate;
@end


@implementation ImageDownloader

- (void)dealloc
{
    [_receiveData release];
    [_urlString release];
    [super dealloc];
}

+ (ImageDownloader *)imageDownloaderWithURLString:(NSString *)urlString delegate:(id<ImageDownloaderDelegate>)delegate tag:(NSInteger)tag
{
    return [[[ImageDownloader alloc] initWithURLString:urlString delegate:delegate tag:tag] autorelease];
}

+ (ImageDownloader *)imageDownloaderWithURLString:(NSString *)urlString delegate:(id<ImageDownloaderDelegate>)delegate
{
    return [[[ImageDownloader alloc] initWithURLString:urlString delegate:delegate] autorelease];
}

- (instancetype)initWithURLString:(NSString *)urlString delegate:(id<ImageDownloaderDelegate>)delegate tag:(NSInteger)tag
{
    if (self=[super init])
    {
        self.urlString = urlString;
        self.delegate = delegate;
        self.tag = tag;
        NSURL *url = [NSURL URLWithString:urlString];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [request release];
        [connection release];
    }
    return self;
}

- (instancetype)initWithURLString:(NSString *)urlString delegate:(id<ImageDownloaderDelegate>)delegate
{
    if (self=[super init])
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.urlString = urlString;
            self.delegate = delegate;
            self.tag = 0;
            NSURL *url = [NSURL URLWithString:urlString];
            NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
            NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
            [request release];
            [connection release];
        });
    }
    return self;
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.receiveData = [NSMutableData data];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receiveData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    UIImage *image = [UIImage imageWithData:self.receiveData];
    if ([_delegate respondsToSelector:@selector(imageDownloader:didiFinshLoadImage:)]) {
        [_delegate imageDownloader:self didiFinshLoadImage:image];
    }
    //NSLog(@"%@下载完毕 %@",self.urlString,image);

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    if ([_delegate respondsToSelector:@selector(imageDownloader:failWithError:)]) {
        [_delegate imageDownloader:self failWithError:error];
    }
    //NSLog(@"\"%@\"下载失败,原因:%@",self.urlString,[error localizedDescription]);
    self.receiveData = nil;
}


@end
